﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

using Dxr.Police.Models;

using Zongsoft.Data;
using Zongsoft.Web.Http;
using Zongsoft.Security.Membership;

namespace Dxr.Police.Web.Http.Controllers
{
    [Authorization(AuthorizationMode.Identity)]
    public abstract class ControllerBase<TModel, TConditional, TService> : ApiController
        where TModel : class
        where TConditional : class, IConditional
        where TService : class, IDataService<TModel>
    {
        #region 成员字段
        private TService _dataService;
        private Zongsoft.Services.IServiceProvider _serviceProvider;
        #endregion

        #region 构造函数
        public ControllerBase(Zongsoft.Services.IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider ?? throw new ArgumentNullException("serviceProvider");
        }
        #endregion

        #region 属性定义
        public UserInfo UserInfo
        {
            get
            {
                var principal = RequestContext.Principal as Principal;
                if (principal == null)
                    return null;

                var identity = principal.Identity as Identity;
                if (identity == null)
                    return null;

                return identity.UserInfo;
            }
        }

        [Zongsoft.Services.ServiceDependency]
        protected TService DataService
        {
            get
            {
                return _dataService;
            }
            set
            {
                _dataService = value ?? throw new ArgumentNullException();
            }
        }

        protected Zongsoft.Services.IServiceProvider ServiceProvider
        {
            get
            {
                return _serviceProvider;
            }
            set
            {
                _serviceProvider = value ?? throw new ArgumentNullException();
            }
        }
        #endregion

        #region 公共方法
        [HttpPaging]
        public virtual HttpResult Get(string id)
        {
            HttpResult result = new HttpResult()
            {
                ret = HttpResult.Success
            };

            if (string.IsNullOrEmpty(id))
            {
                result.ret = HttpResult.Error;
                result.msg = "请传入正确的参数！";
            }

            result.content = this.DataService.Get<string>(id);

            if (result.content == null)
            {
                result.ret = HttpResult.Warn;
                result.msg = "您要找的数据不存在！";
            }

            result.content = ToDisplayModel((TModel)result.content);

            return result;
        }

        public virtual HttpResult Delete(string id)
        {
            HttpResult result = new HttpResult()
            {
                ret = HttpResult.Success
            };

            if (string.IsNullOrEmpty(id))
            {
                result.ret = HttpResult.Error;
                result.msg = "请传入正确的参数！";
            }

            if (this.DataService.Delete<string>(id) < 1)
            {
                result.ret = HttpResult.Warn;
                result.msg = "您要删除的数据不存在！";
            }

            return result;
        }

        public virtual HttpResult Put(TModel model)
        {
            HttpResult result = new HttpResult()
            {
                ret = HttpResult.Success
            };

            if (model == null || (!this.ModelState.IsValid))
            {
                result.ret = HttpResult.Error;
                result.msg = "请传入正确的参数！";
            }

            if (this.DataService.Update(model) < 1)
            {
                result.ret = HttpResult.Warn;
                result.msg = "您要修改的数据不存在！";
            }

            return result;
        }

        public virtual HttpResult Post(TModel model)
        {
            HttpResult result = new HttpResult()
            {
                ret = HttpResult.Success
            };

            if (model == null || (!this.ModelState.IsValid))
            {
                result.ret = HttpResult.Error;
                result.msg = "请传入正确的参数！";
            }

            if (this.DataService.Insert(model) > 0)
                result.content = ToDisplayModel(model);
            else
            {
                result.ret = HttpResult.Error;
                result.msg = "添加失败！";
            }

            return result;
        }

        [HttpPatch, HttpPut]
        public virtual HttpResult Patch(string id, [FromContent]IDictionary<string, object> data)
        {
            HttpResult result = new HttpResult()
            {
                ret = HttpResult.Success
            };

            if (string.IsNullOrWhiteSpace(id) || data == null)
            {
                result.ret = HttpResult.Error;
                result.msg = "请传入正确的参数！";
            }

            if (this.DataService.Update<string>(data, id) < 1)
            {
                result.ret = HttpResult.Warn;
                result.msg = "您要修改的数据不存在！";
            }

            return result;
        }

        [HttpPost]
        [HttpPaging]
        public virtual HttpResult Query(TConditional conditional, [FromUri]Paging paging = null)
        {
            HttpResult result = new HttpResult()
            {
                ret = HttpResult.Success
            };

            var list = this.DataService.Select(conditional, paging).Select(p => ToDisplayModel(p));
            
            result.content = list;

            return result;
        }
        #endregion

        #region 保护方法
        protected abstract object ToDisplayModel(TModel model);
        #endregion
    }
}
